--Los colores
white = Color.new(255,255,255)
black = Color.new(0,0,0)
red = Color.new(255,0,0)
blue = Color.new(0,0,255)
green = Color.new(0,255,0)
clear = Color.new(0,255,0,0)
trans = Color.new(255,255,255,120)
logo = Image.load("music.png")
Image.unswizzle(logo)
bg = Image.load("BG.png")
--Image.unswizzle(bg)
cpuspeed = 200
cpuspeed2 = 70
--Directorio de trabajo
workingdir = System.currentDirectory()
mp3list = {}
--Directorio de los Mp3
dirtable = System.listDirectory("ms0:/Music/")
lengthoflist = table.getn(dirtable)
cur = 1
--Carga de los archivos con extension ".mp3"
for i = 3,lengthoflist do
	if string.lower(string.sub(dirtable[i].name, -4)) == ".mp3" then
		mp3list[cur] = dirtable[i].name
		cur = cur + 1
	end
end

visview = false
mp3listtotal = cur - 1
iconlist = {}

if relaunch == 0 or not relaunch then
	
	dofile("functions.lua") --Script de las funciones
		
	mp3folder = "MUSIC/" --ms0:/MUSIC
	coverfolder = mp3folder.."covers/" --Directorio Covers
	System.setCpuSpeed(333) --Velocidad de la CPU al máximo para cargar lo antes posible las caratulas
	covers = getcovers(mp3listtotal,coverfolder,mp3folder,1,logo,bg) --Cargamos las caratulas
	System.setCpuSpeed(222) --Bajamos la velocidad de la CPU, ya no es necesario que esté al máximo
	dis = 2 --Distancia entre cancion y cancio (entiendase distancia entre las imagenes)
	coverflow_first = 0
	
	playing = false --No esta reproduciendo aun
	--Distancia entre los covers
	wall = {
		{0,0,white,-0.5,0.5,dis},
		{1,0,white,0.5,0.5,dis},
		{0.5,0.5,white,0,0,dis},
		
		{1,0,white,0.5,0.5,dis},
		{1,1,white,0.5,-0.5,dis},
		{0.5,0.5,white,0,0,dis},
		
		{1,1,white,0.5,-0.5,dis},
		{0,1,white,-0.5,-0.5,dis},
		{0.5,0.5,white,0,0,dis},
		
		{0,1,white,-0.5,-0.5,dis},
		{0,0,white,-0.5,0.5,dis},
		{0.5,0.5,white,0,0,dis}
	}
	wall2 = {
		{0,0.5,white,0.5,0,dis},
		{0,1,white,0.5,-0.5,dis},
		{0.5,0.5,white,0,0,dis},
		
		{0,0.5,white,0.5,0,dis},
		{0,1,white,0.5,-0.5,dis},
		{0.5,0.5,white,0,0,dis},
		
		{0,1,white,0.5,-0.5,dis},
		{1,1,white,-0.5,-0.5,dis},
		{0.5,0.5,white,0,0,dis},
		
		{1,1,white,-0.5,-0.5,dis},
		{1,0.5,white,-0.5,0,dis},
		{0.5,0.5,white,0,0,dis}
	}
	plane = {
		{0,0.5,white,-8,3,-4},
		{0,1,white,-8,-3,-4},
		{0.5,0.5,white,8,3,-4},
		
		{0,0.5,white,-8,-3,-4},
		{0,1,white,8,-3,-4},
		{0.5,0.5,white,8,3,-4},
		
	   --[[{0,1,white,0.5,-0.5,dis},
		{1,1,white,-0.5,-0.5,dis},
		-0.5,0.5,white,0,0,dis},
		
		{1,1,white,-0.5,-0.5,dis},
		{1,0.5,white,-0.5,0,dis},
		{0.5,0.5,white,0,0,dis}]]
	}
	--Movimiento de las canciones
	xaxis = (4*#covers-4)
	oldx = xaxis-1 
	oldoldx = oldx-1
	zaxis = -3.3
	filter = 1
	current = 0
	
	maxxaxis = 4*#covers-4
	
	onestep = ((maxxaxis+4)/#covers)
	currentp = (4*#covers-4)	
	
	cplaying = 0
	
	eos = {fmp3 = 1, f = 1}
end


relaunch = 0

oldpad = Controls.read()
message = false
while true do --Bucle principal
	pad = Controls.read()
	newx = 0
	screen.startDraw() --Iniciamos los graficos
	start_gu() --Iniciamos las funciones de 3D
	Image.blit(0,0,bg) --Mostramos la imagen de fondo
	if playing == true then --Si esta reproduciendo entonces...
		if Mp3me.eos() == true then
			currentp = currentp - onestep
				Mp3me.stop()
				current = current - 1
				if current < 1 then
					current = mp3listtotal
					currentp = (4*#covers-4)
				end
				Mp3me.load("ms0:/Music/"..mp3list[current])
				Mp3me.play()
		end
		for i = 1,240 do --Muestra las lineas de fondo en movimiento
				color = Color.new(255,194+i,14) --Color de las lineas (R,G,B) en degradado 
				color1 = Color.new(255,194-i,14) --Color de las lineas (R,G,B) en degradado 
			--Image.drawLine(i*2,130,i*2,Mp3me.visR(i*3),color)
		Image.drawLine(239,160,i*2,Mp3me.visR(i),color1)--Funcion de dibujar las lineas en pantalla
		end --Posiblemente usare este metodo de color para futuras versiones en el escritorio
	end
	if pad:l() then --Pasa a la cancion anterior si es que hay una
		if not oldpad:l() then
			currentp = currentp + onestep
			Mp3me.stop()
			current = current + 1
			if current > mp3listtotal then
				current = mp3listtotal
				currentp = (4*#covers-4)
			end
			Mp3me.load("ms0:/Music/"..mp3list[current])
			Mp3me.play()
			playing = true
		end
	end
	if pad:r() then --Pasa a la cancion siguiente si es que hay una
		if not oldpad:r() then
			currentp = currentp - onestep
			Mp3me.stop()
			current = current - 1
			if current < 1 then
				current = mp3listtotal
				currentp = (4*#covers-4)
			end
			Mp3me.load("ms0:/Music/"..mp3list[current])
			Mp3me.play()
			playing = true
		end
	end
	--Mueve una cancion hacia la derecha
	if pad:left() and not oldpad:left() then
			currentp = currentp + onestep
	end
	--Mueve una cancion hacia la izquierda
	if pad:right() and not oldpad:right() then
			currentp = currentp - onestep
	end
	--Velocidad de movimiento
	if xaxis < currentp then
		xaxis = xaxis + 0.5
		newx = 1
	end
	if xaxis > currentp then
		xaxis = xaxis - 0.5
		newx = 1
	end
	--if oldx ~= xaxis or oldx ~= oldoldx or pad:square() or pad:triangle() then --> Realmente no necesario
		if xaxis < 0 then
			xaxis = 0
			currentp = 0
		end
		if xaxis > maxxaxis then
			xaxis = maxxaxis
			currentp = maxxaxis
		end
		for i = 1, mp3listtotal do
			covers[i].pos = (i-1)*(-4)+xaxis
			if covers[i].pos > -8.2 and covers[i].pos < 8.2 then
				covers[i].rot = -covers[i].pos/4.1
				dp(covers[i].pos,0.3,zaxis,0,covers[i].rot,covers[i].pos2,wall,covers[i].img,filter)
			end
		end
		current = math.floor((xaxis-2)/4)+2
		screen.print(315,245,covers[current].name,0.6,white,0)
		screen.print(315,260,covers[current].artist,0.6,white,0)
	--end
	--[[if playing == true then
		lo que quiera que ocurra :P
	end]]
	screen.endDraw() --Acabamos con los graficos
	--screen.waitVblankStart()
	screen.flipscreen() --Damos la vuelta  a la pantalla
	if pad:cross() then --Si pulsamos X se reproduce la cancion seleccionada
		if not oldpad:cross() then
			Mp3me.stop()
			Mp3me.load("ms0:/Music/"..mp3list[current])
			Mp3me.play()
			playing = true
		end
	end
	--Al pulsar [] se pausa la cancion en reproduccion
	if pad:square() then
		if not oldpad:square() then
			Mp3me.pause()
		end
	end
	--Al pulsar O se para la cancion en reproduccion
	if pad:circle() then
		if not oldpad:circle() then
			Mp3me.stop()
			playing = false
		end
	end
	oldoldx = oldx
	oldx = xaxis
	oldpad = pad
	if pad:start() then --Al escritorio
		System.Quit()
	end
end